package com.laker.autotest.webui.page.gitee;


import com.laker.autotest.webui.core.BasePage;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.assertions.LocatorAssertions;
import com.microsoft.playwright.options.AriaRole;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

/**
 * <p>
 * https://playwright.dev/java/docs/pom
 * 页面对象模型，目的是为了代码维护方便。
 * 以搜索功能为例，搜索页面需要存放元素定位和控件操作行为。
 * gitee 搜索页
 * </p>
 */
public class GiteeSearchPage extends BasePage {
    private static final String URL = "https://search.gitee.com";

    private final Locator searchInput;

    public GiteeSearchPage(Page page) {
        super(page);
        searchInput = page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("在 Gitee 上搜开源项目"));
    }

    @Override
    protected String initUrl() {
        return URL;
    }

    public void searchForProject(String projectName) {
        searchInput.click();
        searchInput.fill(projectName);
        searchInput.press("Enter");
    }

    public GiteeRepoPage openProjectPage(String projectName) {
        Page page1 = page.waitForPopup(() -> {
            page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName(projectName)).click();
        });
        return new GiteeRepoPage(page1);
    }

    public void assertProjectVisible(String projectName) {
        assertThat(page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName(projectName))).isVisible(
                new LocatorAssertions.IsVisibleOptions()
                        .setTimeout(10000) // 超时时间
                        .setVisible(true) // 是否可见
        );
    }
}
